#coding=utf-8
'''
@文件    :network_back.py
@说明    :
@时间    :2020/04/01 17:37:09
@作者    :didiplus
@版本    :1.0
'''

# 通过telnet 远程登录设备

import telnetlib
import os
import datetime
import time
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

time1=datetime.datetime.now()
time2=time1.strftime('%Y%m%d')

def path_check(path):
      if os.path.exists(path):
            pass
      else:
            os.mkdir(path)

def login_ftp(telnetsession,password_prompt):
            telnetsession.write("ftp 192.168.1.1\n") #ftp服务器IP
            time.sleep(1.5)
            telnetsession.read_until("(none)):")
            telnetsession.write("admin\n")  #ftp用户名
            time.sleep(1.5)
            telnetsession.read_until(password_prompt)
            telnetsession.write("nomore532\n") #ftp用户名和密码
            time.sleep(1.5)
            telnetsession.read_until("[ftp]")
            telnetsession.write("cd "+ time2 +"\n")
            telnetsession.read_until("[ftp]")



def do_telent(Host,username,password):
      _UsermodTag=">"
      telnetsession = telnetlib.Telnet(Host,port=23,timeout=5)
      telnetsession.set_debuglevel(2)
      time1=datetime.datetime.now()
      time2=time1.strftime('%Y-%m-%d')

      #输入用户名
      login_prompt="Username"
      response = telnetsession.read_until(login_prompt)
      if login_prompt in response:
            print ('[*] Username: %s'%username)
            telnetsession.write(username + "\n")

      #输入密码 
      password_prompt="Password"
      response = telnetsession.read_until(password_prompt)
      if password_prompt in response:
            print ('[*] Password: %s'%password)
            telnetsession.write(password + "\n") 

      telnetsession.read_until(_UsermodTag)
      H3C_SW=["10.91.74.45","10.91.74.46","10.91.74.47"]
      if (Host in H3C_SW):
            telnetsession.write("super" +"\n")
            telnetsession.read_until(password_prompt)
            telnetsession.write("admin123" + "\n")
            telnetsession.read_until(_UsermodTag)
            login_ftp(telnetsession,password_prompt)
            telnetsession.write("put startup.cfg %s" %Host+'_'+time2+"_startup.cfg" + "\n")
      elif (Host == "10.91.74.34"):
            telnetsession.write("\n")
            telnetsession.read_until(_UsermodTag)
            login_ftp(telnetsession,password_prompt)
            telnetsession.write("put vrpcfg-2n.cfg %s" %Host+'_'+time2+"_vrpcfg-2n.cfg" + "\n")
            telnetsession.read_until('[ftp]')

      elif (Host == "10.91.74.33"):
            telnetsession.write("\n")
            telnetsession.read_until(_UsermodTag)
            login_ftp(telnetsession,password_prompt)
            telnetsession.write("put vrpcfg-1n.cfg %s" %Host+'_'+time2+"_vrpcfg-1n.cfg" + "\n")
            telnetsession.read_until('[ftp]')
      elif (Host == "10.91.74.62"):
            telnetsession.write("super" +"\n")
            telnetsession.read_until(password_prompt)
            telnetsession.write("admin123" + "\n")
            telnetsession.read_until(_UsermodTag)
            password_prompt="password"
            login_ftp(telnetsession,password_prompt)
            telnetsession.write("dir\n")
            telnetsession.read_until("[ftp]")
            telnetsession.write("put vrpcfg.zip %s" %Host+'_'+time2+"_vrpcfg.zip\n")
            telnetsession.read_until("[ftp]")
            #

      else:
            login_ftp(telnetsession,"password")
            telnetsession.write("put vrpcfg.zip %s" %Host+'_'+time2+"_vrpcfg.zip" + "\n")


      telnetsession.close()

if __name__ == '__main__':
      path="D:\\backup\\"+time2
      path_check(path)
      Host=["192.168.1.10","192.168.1.11","192.168.1.12","192.168.1.13","192.168.1.14",]
      username="admin"
      
      for host in Host:
            if host in ["10.91.74.33","10.91.74.34"]:
                  password="*******"
                  do_telent(host,username,password)
            elif(host == "10.91.74.62"):
                  password="*******"
                  do_telent(host,username,password)
            else:
                  password="*******"
                  do_telent(host,username,password)




